-- spell_bonus
-- created by panyl
-- 进入下一层时，获得x本a~b阶的魔法卷轴

return {
    apply = function(prop, baseValue)
        return baseValue + prop[3];
    end,

    -- 进入下一层触发
    trigger = function(who, prop, arg)
        local range = string.explode(prop[2], "~");
        local min, max;

        if #range > 1 then
            min = tonumber(range[1]);
            max = tonumber(range[2]);
        else
            min = tonumber(range[1]);
            max = min;
        end

        -- 卷轴阶位增加属性
        local p = PropM.combine(who, "spell_step_add", 1);
        min = min + math.modf(p[3] / 2);
        max = max + p[3];

        assert(max >= min);

        local count = tonumber(prop[3]);
        local differ = max - min;

        for i = 1, count do
            local rand = DungeonM.getRandSeed("spell_bonus");
            local  rank = min + rand % (differ + 1);

            local spells = SpellM.getSpellByRank(rank);

            rand = DungeonM.getRandSeed("spell_bonus");
            local spell = spells[rand % #spells + 1];

            BonusM.doBonus({ 1, spell, 1, }, "spell bonus");
        end

        return true;
    end,

    desc = function(prop)
        local row = PropM.getRow(prop[1]);
        local desc = row.desc;

        local range = string.explode(prop[2], "~");
        local min, max;

        if #range > 1 then
            min = tonumber(range[1]);
            max = tonumber(range[2]);
        else
            min = tonumber(range[1]);
            max = min;
        end

        -- 卷轴阶位增加属性
        local p = PropM.combine(ME.user, "spell_step_add", 1);
        min = min + math.modf(p[3] / 2);
        max = max + p[3];

        desc = string.gsub(desc, "{0}", prop[3]);
        if max == min then
            desc = string.gsub(desc, "{1}", max);
        else
            desc = string.gsub(desc, "{1}", tostring(min) .. "~" .. tostring(max));
        end

        desc = string.gsub(desc, "~", getLocStr("hyphen"));

        return desc;
    end,

    record = function(target, prop, path)
        return false;
    end
};
